import { getPermissionAPI, getRoutesAPI } from '@/api/login'
import router, { DynamicRoutes } from '@/router/index.js'
import dynamicRouter from '@/router/dynamic-router'
import getTree from '@/utils/getTree'
import { recursionRouter, setDefaultRoute } from '@/utils/recursion-route'
import getRoutes from '@/utils/getRoutes'

export default {
    namespaced: true,
    state: {
        permissionList: [],
        sidebarMenu: [],
        currentMenu: sessionStorage.getItem('currentMenu') || ""
    },
    getters: {},
    mutations: {
        SET_PERMISSION(state, routes) {

            state.permissionList = routes

        },
        CLEAT_PERMISSiON(state) {
            state.permissionList = []
        },
        SET_MENU(state, menu) {
            state.sidebarMenu = menu
        },
        CLEAR_MENU(state) {
            state.sidebarMenu = []
        },
        SET_CURRENTMENU(state, data) {
            state.currentMenu = data
            sessionStorage.setItem('currentMenu', data)
        }
    },
    actions: {
        async FETCH_PERMISSION({ commit, state },) {

            let premissionList = await getPermissionAPI()
            console.log(premissionList);

            let children = []
            let MainContainer = { children: [] }
            let routes = []
            routes = recursionRouter(premissionList.data, dynamicRouter)
          

            MainContainer = DynamicRoutes.find(v => v.name == '/')

            MainContainer.children = []
            children = MainContainer.children
            children.push(...routes)
            // 生成菜单

            commit('SET_MENU', children)
            // 设置默认路由
            setDefaultRoute([MainContainer])
            // 初始化路由
            let initRoutes = router.options.routes
            DynamicRoutes.forEach(v => {
                router.addRoute(v)
            })
           

            commit('SET_PERMISSION', [...initRoutes, ...DynamicRoutes])
        },

        // 获取路由
        async GETROUTERS({ commit, state },) {

            let premissionList = await getRoutesAPI()



            let routesTree = getTree(premissionList)
            // commit('SET_MENU', routesTree)
            let routes = getRoutes(routesTree)


            // let routes = recursionRouter(premissionList.data, dynamicRouter)
            let MainContainer = DynamicRoutes.find(v => v.name == '/')

            let children = MainContainer.children
            children.push(...routes)
            // 生成菜单
            console.log(children, '菜单')
            commit('SET_MENU', children)
            // 设置默认路由
            setDefaultRoute([MainContainer])
            // 初始化路由
            let initRoutes = router.options.routes
            console.log(DynamicRoutes);
            DynamicRoutes.forEach(v => {
                router.addRoute(v)
            })

            commit('SET_PERMISSION', [...initRoutes, ...DynamicRoutes])
            // commit('SET_PERMISSION', DynamicRoutes)



            // commit('SET_PERMISSION', routes)

        },
        // 退出
        logout({ commit, state }) {

            commit('CLEAR_MENU')
            commit('CLEAT_PERMISSiON')
            let BASE_URL = sessionStorage.getItem('BASE_URL')
            sessionStorage.clear()
            sessionStorage.setItem('BASE_URL', BASE_URL)

            // router.replace('/login')
            router.push('/login')

        }
    }
}